//-----------------------------------------------------------------------
// <copyright file="a_CreditCardDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CreditCardDAOBase
    {
        #region Common methods
        public virtual a_CreditCard Createa_CreditCardFromReader(IDataReader reader)
        {
            a_CreditCard item = new a_CreditCard();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CCID"))) item.CCID = (int)reader["CCID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CardType"))) item.CardType = (string)reader["CardType"];
                if (!reader.IsDBNull(reader.GetOrdinal("CardNum"))) item.CardNum = (string)reader["CardNum"];
                if (!reader.IsDBNull(reader.GetOrdinal("ExpireMonth"))) item.ExpireMonth = (string)reader["ExpireMonth"];
                if (!reader.IsDBNull(reader.GetOrdinal("ExpireYear"))) item.ExpireYear = (string)reader["ExpireYear"];
                if (!reader.IsDBNull(reader.GetOrdinal("NameOnCard"))) item.NameOnCard = (string)reader["NameOnCard"];
                if (!reader.IsDBNull(reader.GetOrdinal("Firstname"))) item.Firstname = (string)reader["Firstname"];
                if (!reader.IsDBNull(reader.GetOrdinal("Lastname"))) item.Lastname = (string)reader["Lastname"];
                if (!reader.IsDBNull(reader.GetOrdinal("SecurityCode"))) item.SecurityCode = (string)reader["SecurityCode"];
                if (!reader.IsDBNull(reader.GetOrdinal("IsDeleted"))) item.IsDeleted = (byte)reader["IsDeleted"];
                if (!reader.IsDBNull(reader.GetOrdinal("DomainID"))) item.DomainID = (int)reader["DomainID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_CreditCard methods
            
        public virtual void Createa_CreditCard(a_CreditCard a_CreditCard)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CreditCards_ins");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_CreditCard.UserID);
                database.AddInParameter(dbCommand, "@CardType", DbType.AnsiString, a_CreditCard.CardType);
                database.AddInParameter(dbCommand, "@CardNum", DbType.AnsiString, a_CreditCard.CardNum);
                database.AddInParameter(dbCommand, "@ExpireMonth", DbType.AnsiString, a_CreditCard.ExpireMonth);
                database.AddInParameter(dbCommand, "@ExpireYear", DbType.AnsiString, a_CreditCard.ExpireYear);
                database.AddInParameter(dbCommand, "@NameOnCard", DbType.AnsiString, a_CreditCard.NameOnCard);
                database.AddInParameter(dbCommand, "@Firstname", DbType.AnsiString, a_CreditCard.Firstname);
                database.AddInParameter(dbCommand, "@Lastname", DbType.AnsiString, a_CreditCard.Lastname);
                database.AddInParameter(dbCommand, "@SecurityCode", DbType.AnsiStringFixedLength, a_CreditCard.SecurityCode);
                database.AddInParameter(dbCommand, "@IsDeleted", DbType.Byte, a_CreditCard.IsDeleted);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_CreditCard.DomainID);
                database.AddOutParameter(dbCommand, "@CCID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_CreditCard.CCID = (int)database.GetParameterValue(dbCommand, "@CCID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_CreditCard methods
        
        public virtual void Updatea_CreditCard(a_CreditCard a_CreditCard)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CreditCards_upd");            
                
                database.AddInParameter(dbCommand, "@CCID", DbType.Int32, a_CreditCard.CCID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_CreditCard.UserID);
                database.AddInParameter(dbCommand, "@CardType", DbType.AnsiString, a_CreditCard.CardType);
                database.AddInParameter(dbCommand, "@CardNum", DbType.AnsiString, a_CreditCard.CardNum);
                database.AddInParameter(dbCommand, "@ExpireMonth", DbType.AnsiString, a_CreditCard.ExpireMonth);
                database.AddInParameter(dbCommand, "@ExpireYear", DbType.AnsiString, a_CreditCard.ExpireYear);
                database.AddInParameter(dbCommand, "@NameOnCard", DbType.AnsiString, a_CreditCard.NameOnCard);
                database.AddInParameter(dbCommand, "@Firstname", DbType.AnsiString, a_CreditCard.Firstname);
                database.AddInParameter(dbCommand, "@Lastname", DbType.AnsiString, a_CreditCard.Lastname);
                database.AddInParameter(dbCommand, "@SecurityCode", DbType.AnsiStringFixedLength, a_CreditCard.SecurityCode);
                database.AddInParameter(dbCommand, "@IsDeleted", DbType.Byte, a_CreditCard.IsDeleted);
                database.AddInParameter(dbCommand, "@DomainID", DbType.Int32, a_CreditCard.DomainID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_CreditCard methods
        public virtual void Deletea_CreditCard(int ccid)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CreditCards_del");
                
                database.AddInParameter(dbCommand, "@CCID", DbType.Int32, ccid);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_CreditCard methods
        
        public virtual a_CreditCard Geta_CreditCard(int ccid)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CreditCards_sel");
                
                database.AddInParameter(dbCommand, "@CCID", DbType.Int32, ccid);
                
                a_CreditCard a_CreditCard =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CreditCard = Createa_CreditCardFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CreditCard;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CreditCardList methods
        public virtual a_CreditCardCollection Geta_CreditCardList(a_CreditCardColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CreditCards_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CreditCardCollection a_CreditCardCollection = new a_CreditCardCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CreditCard a_CreditCard = Createa_CreditCardFromReader(reader);
                        a_CreditCardCollection.Add(a_CreditCard);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CreditCardCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CreditCardCollection Geta_CreditCardList(a_CreditCardColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CreditCardList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}